﻿using System;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
namespace AnyOffice.general.system.qikan
{
    public partial class edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["disk_id"] == null) ? 0 : int.Parse(Request.QueryString["disk_id"].ToString());
                this.ViewState["DiskId"] = num;
                this.ViewState["Type"] = "0";
                DataTable table = new QikanDao().SelectByDiskId(base.Conn, num);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowError("无此知识目录！", "index.aspx");
                    return;
                }
                this.txtName.Value = (table.Rows[0]["Name"] != DBNull.Value) ? table.Rows[0]["Name"].ToString() : "";
                this.ViewState["Type"] = table.Rows[0]["Type"].ToString();
                if (this.ddlType.Items.FindByValue(table.Rows[0]["Type"].ToString()) != null)
                {
                    this.ddlType.Items.FindByValue(table.Rows[0]["Type"].ToString()).Selected = true;
                }
                this.PostDeptId.Value = (table.Rows[0]["PostDept"] != DBNull.Value) ? table.Rows[0]["PostDept"].ToString() : "";
                string str = (table.Rows[0]["PostDeptName"] != DBNull.Value) ? table.Rows[0]["PostDeptName"].ToString() : "";
                if (str != string.Empty)
                {
                    str = str + ",";
                }
                this.PostDeptName.Value = str;
                this.AccessUserId.Value = (table.Rows[0]["AccessUser"] != DBNull.Value) ? table.Rows[0]["AccessUser"].ToString() : "";
                string str2 = (table.Rows[0]["AccessUserName"] != DBNull.Value) ? table.Rows[0]["AccessUserName"].ToString() : "";
                if (str2 != string.Empty)
                {
                    str2 = str2 + ",";
                }
                this.AccessUserName.Value = str2;
                string str3 = (table.Rows[0]["DefaultPriv"] != DBNull.Value) ? table.Rows[0]["DefaultPriv"].ToString() : "";
                foreach (ListItem item in this.ckblDefaultPriv.Items)
                {
                    if (str3.IndexOf(item.Value) > -1)
                    {
                        item.Selected = true;
                    }
                }
                this.NewUserId.Value = (table.Rows[0]["NewUser"] != DBNull.Value) ? table.Rows[0]["NewUser"].ToString() : "";
                string str4 = (table.Rows[0]["NewUserName"] != DBNull.Value) ? table.Rows[0]["NewUserName"].ToString() : "";
                if (str4 != string.Empty)
                {
                    str4 = str4 + ",";
                }
                this.NewUserName.Value = str4;
                this.ReadUserId.Value = (table.Rows[0]["ReadUser"] != DBNull.Value) ? table.Rows[0]["ReadUser"].ToString() : "";
                string str5 = (table.Rows[0]["ReadUserName"] != DBNull.Value) ? table.Rows[0]["ReadUserName"].ToString() : "";
                if (str5 != string.Empty)
                {
                    str5 = str5 + ",";
                }
                this.ReadUserName.Value = str5;
                string str6 = (table.Rows[0]["ReadPriv"] != DBNull.Value) ? table.Rows[0]["ReadPriv"].ToString() : "";
                foreach (ListItem item2 in this.ckblReadPriv.Items)
                {
                    if (str6.IndexOf(item2.Value) > -1)
                    {
                        item2.Selected = true;
                    }
                }
                this.EditUserId.Value = (table.Rows[0]["EditUser"] != DBNull.Value) ? table.Rows[0]["EditUser"].ToString() : "";
                string str7 = (table.Rows[0]["EditUserName"] != DBNull.Value) ? table.Rows[0]["EditUserName"].ToString() : "";
                if (str7 != string.Empty)
                {
                    str7 = str7 + ",";
                }
                this.EditUserName.Value = str7;
                this.DownloadUserId.Value = (table.Rows[0]["DownloadUser"] != DBNull.Value) ? table.Rows[0]["DownloadUser"].ToString() : "";
                string str8 = (table.Rows[0]["DownloadUserName"] != DBNull.Value) ? table.Rows[0]["DownloadUserName"].ToString() : "";
                if (str8 != string.Empty)
                {
                    str8 = str8 + ",";
                }
                this.DownloadUserName.Value = str8;
                this.ManageUserId.Value = (table.Rows[0]["ManageUser"] != DBNull.Value) ? table.Rows[0]["ManageUser"].ToString() : "";
                string str9 = (table.Rows[0]["ManageUserName"] != DBNull.Value) ? table.Rows[0]["ManageUserName"].ToString() : "";
                if (str9 != string.Empty)
                {
                    str9 = str9 + ",";
                }
                this.ManageUserName.Value = str9;
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                QikanDao dao2 = new QikanDao();
                QikanData data = new QikanData();
                data.DiskId = Convert.ToInt32(this.ViewState["DiskId"]);
                data.Name = this.txtName.Value;
                data.Type = Convert.ToInt32(this.ddlType.Value);
                if (this.PostDeptId.Value != string.Empty)
                {
                    data.PostDept = this.PostDeptId.Value;
                }
                if (this.AccessUserId.Value != string.Empty)
                {
                    data.AccessUser = this.AccessUserId.Value;
                }
                string s = string.Empty;
                foreach (ListItem item3 in this.ckblDefaultPriv.Items)
                {
                    if (item3.Selected)
                    {
                        s = s + item3.Value + ",";
                    }
                }
                if (s != string.Empty)
                {
                    data.DefaultPriv = s;
                }
                Response.Write(s);
                if (this.NewUserId.Value != string.Empty)
                {
                    data.NewUser = this.NewUserId.Value;
                }
                if (this.ReadUserId.Value != string.Empty)
                {
                    data.ReadUser = this.ReadUserId.Value;
                }
                string str11 = string.Empty;
                foreach (ListItem item4 in this.ckblReadPriv.Items)
                {
                    if (item4.Selected)
                    {
                        str11 = str11 + item4.Value + ",";
                    }
                }
                if (str11 != string.Empty)
                {
                    data.ReadPriv = str11;
                }
                if (this.EditUserId.Value != string.Empty)
                {
                    data.EditUser = this.EditUserId.Value;
                }
                if (this.DownloadUserId.Value != string.Empty)
                {
                    data.DownloadUser = this.DownloadUserId.Value;
                }
                if (this.ManageUserId.Value != string.Empty)
                {
                    data.ManageUser = this.ManageUserId.Value;
                }
                try
                {
                    dao2.Update(base.Conn, data);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.litMessage.Text = base.ShowError("知识目录修改失败！");
                    return;
                }
                base.CloseConn();
                Response.Redirect("index.aspx");
            }
        }
    }
}
